package com.xuecheng.manage_cms.service.impl;

import com.xuecheng.framework.domain.cms.CmsSiteServer;
import com.xuecheng.framework.domain.cms.request.QuerySiteServerRequest;
import com.xuecheng.framework.domain.cms.response.CmsCode;
import com.xuecheng.framework.domain.cms.response.CmsSiteServerCode;
import com.xuecheng.framework.domain.cms.response.SiteServerResult;
import com.xuecheng.framework.exception.ExceptionCast;
import com.xuecheng.framework.model.response.CommonCode;
import com.xuecheng.framework.model.response.QueryResponseResult;
import com.xuecheng.framework.model.response.QueryResult;
import com.xuecheng.framework.model.response.ResponseResult;
import com.xuecheng.manage_cms.dao.CmsSiteServerRepository;
import com.xuecheng.manage_cms.service.CmsSiteServerService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.*;
import org.springframework.stereotype.Service;

import java.util.Optional;

@Service
public class CmsSiteServerServiceImpl implements CmsSiteServerService {

    //注入
    @Autowired
    private CmsSiteServerRepository cmsSiteServerRepository;

    @Override
    public QueryResponseResult findList(int page, int size, QuerySiteServerRequest siteServerRequest) {
        if(null == siteServerRequest){
            siteServerRequest = new QuerySiteServerRequest();
        }
        CmsSiteServer cmsSiteServer = new CmsSiteServer();
        //设置ip查询条件
        if(StringUtils.isNotEmpty(siteServerRequest.getIp())){
            cmsSiteServer.setIp(siteServerRequest.getIp());
        }
        //设置端口查询条件
        if(StringUtils.isNotEmpty(siteServerRequest.getPort())){
            cmsSiteServer.setPort(siteServerRequest.getPort());
        }
        //设置服务器名称
        if(StringUtils.isNotEmpty(siteServerRequest.getServerName())){
            cmsSiteServer.setServerName(siteServerRequest.getServerName());
        }
        //设置站点id
        if(StringUtils.isNotEmpty(siteServerRequest.getSiteId())){
            cmsSiteServer.setSiteId(siteServerRequest.getSiteId());
        }

        ExampleMatcher matcher = ExampleMatcher.matching().withMatcher("serverName", ExampleMatcher.GenericPropertyMatchers.contains());
        Example<CmsSiteServer> example = Example.of(cmsSiteServer, matcher);
        //设置分页参数
        if(page <= 0){
            page = 1;
        }
        page -= 1;
        if(size <= 0){
            size = 10;
        }
        Pageable pageable = PageRequest.of(page, size);
        Page<CmsSiteServer> pagedata = cmsSiteServerRepository.findAll(example, pageable);
        QueryResult queryResult = new QueryResult();
        queryResult.setList(pagedata.getContent());
        queryResult.setTotal(pagedata.getTotalElements());
        QueryResponseResult queryResponseResult = new QueryResponseResult(CommonCode.SUCCESS,queryResult);
        return queryResponseResult;
    }

    @Override
    public SiteServerResult add(CmsSiteServer cmsSiteServer) {
        //查询是否存在此ip,此端口,此站点Id的记录
        CmsSiteServer dbCmsSiteServer = cmsSiteServerRepository.findByIpAndPortAndSiteId(cmsSiteServer.getIp(), cmsSiteServer.getPort(), cmsSiteServer.getSiteId());
        if(null != dbCmsSiteServer){
            //已经存在,不允许添加
            ExceptionCast.cast(CmsSiteServerCode.CMS_ADDSITESERVER_EXISTS);
        }
        //主键是自动生成,此处置空
        cmsSiteServer.setServerId(null);
        cmsSiteServerRepository.save(cmsSiteServer);
        return new SiteServerResult(CommonCode.SUCCESS,cmsSiteServer);
    }

    @Override
    public CmsSiteServer findById(String id) {
        Optional<CmsSiteServer> optional = cmsSiteServerRepository.findById(id);
        if(optional.isPresent()){
            CmsSiteServer cmsSiteServer = optional.get();
            return cmsSiteServer;
        }
        return null;
    }

    @Override
    public SiteServerResult eidt(String id, CmsSiteServer cmsSiteServer) {
        //根据id查询
        CmsSiteServer one = this.findById(id);
        if(null == one){
            ExceptionCast.cast(CmsSiteServerCode.CMS_EDITSITESERVER_NOTEXISTS);
        }
        one.setIp(cmsSiteServer.getIp());
        one.setPort(cmsSiteServer.getPort());
        one.setServerName(cmsSiteServer.getServerName());
        one.setSiteId(cmsSiteServer.getSiteId());
        one.setUploadPath(cmsSiteServer.getUploadPath());
        one.setUseType(cmsSiteServer.getUseType());
        one.setWebPath(cmsSiteServer.getWebPath());
        CmsSiteServer save = cmsSiteServerRepository.save(one);
        return new SiteServerResult(CommonCode.SUCCESS,one);
    }

    @Override
    public ResponseResult delete(String id) {
        cmsSiteServerRepository.deleteById(id);
        return new QueryResponseResult(CommonCode.SUCCESS,null);
    }
}
